<template>
  <div>
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="供应商名称">
              <span>{{ props.row.username }}</span>
            </el-form-item>
            <el-form-item label="供应商编号">
              <span>{{ props.row.usernumber }}</span>
            </el-form-item>
            <el-form-item label="采购编号">
              <span>{{ props.row.buynumber }}</span>
            </el-form-item>
            <el-form-item label="商品规格">
              <span>{{ props.row.standerd }}</span>
            </el-form-item>
            <el-form-item label="商品类型">
              <span>{{ props.row.type }}</span>
            </el-form-item>
            <el-form-item label="商品单价">
              <span>{{ props.row.orderprice }}</span>
            </el-form-item>
            <el-form-item label="退货数量">
              <span>{{ props.row.returnsnum }}</span>
            </el-form-item>
            <el-form-item label="退货原因">
              <span>{{ props.row.returncause }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
          label="退货订单编号"
          prop="ordernumber">
      </el-table-column>
      <el-table-column
          label="退货订单日期"
          prop="orderdate">
      </el-table-column>
      <el-table-column
          label="退货商品名"
          prop="goodsname">
      </el-table-column>
      <el-table-column
          label="退货订单状态"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.returntype==0">未退款</span>
          <span v-if="scope.row.returntype==1">退货成功</span>
          <span v-if="scope.row.returntype==2">退货失败</span>
          <span v-if="scope.row.returntype==3">已退款</span>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="退款"
          width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">退款</el-button>
          <el-button @click="handleClick2(scope.row)" type="text" size="small">确认退货</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5,10,15,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="pageTotal">
      </el-pagination>
    </div>
    <el-dialog
        title="退款"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <span>你一共要退款:{{money}}</span><br>
      <span>点击确定代表已退款给系统</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false,money=0">取 消</el-button>
    <el-button type="primary" @click="getMoney">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "ReturnsView",
  data(){
    return{
      tableData:[],
      pageSize:5,
      pageTotal:0,
      currentPage:1,
      dialogVisible:false,
      money:0,
      row:null
    }
  },mounted() {
    this.init();
  },methods:{
    init(){

      this.$axios.get("/psc/return/getReturnsByPage/"+this.currentPage+"/"+this.pageSize+"/"+sessionStorage.getItem("userNumber")+"").then(
          r=>{
            console.log(r.data)
            this.tableData=r.data.list;
            this.pageTotal=r.data.pageTotal;
          }
      )
    },handleClick(row){
      this.row=row;
      this.money=row.returnsnum*row.orderprice;
      this.dialogVisible=true;
    }, handleSizeChange(val){
      this.pageSize=val;
      this.init()
    },handleCurrentChange(val){
      this.currentPage=val
      this.init()
    },getMoney() {
      if(this.row.returntype==0){
        this.$axios.get("/psc/return/returnMoney/" + this.row.ordernumber + "/" + this.row.buynumber + "/" + this.row.returnsnum + "/" + this.money + "").then(
            r => {
              if (r.data) {
                this.$message.success("正在审核退款信息")
              } else {
                this.$message.error("信息发送错误，请联系客户")
              }
            }
        )
      }else {
        this.$message.error("不能进行退款，请查看订单状态")
      }
      this.dialogVisible = false;
    },open(row){
      this.$alert('你确定收到退款货物了嘛？', '退货信息', {
        confirmButtonText: '确定',
        callback: action  => {
          console.log(action)
          this.handleClick2(row);
        }
      });
    },handleClick2(row){
      if(row.returntype==3){
        this.$axios.get("/psc/return/returnOrder/"+row.ordernumber + "/" + row.buynumber+"").then(
            r=>{
              if(r.data){
                this.init();
                this.$message.success("确认退货成功")
              }
            }
        )
      }else {
        this.$message.warning("货物无法确认退货")
      }
    }
  }
}
</script>

<style scoped>

</style>